package com.zx.notification.consumer.util;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zx.notification.common.enums.ChannelTypeEnum;
import com.zx.notification.common.enums.MqTypeEnum;
import com.zx.notification.consumer.config.ChannelConfigManager;
import com.zx.notification.consumer.service.IMessageProcessService;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * 消息处理工具类
 * 用于处理各种MQ监听器中的通用消息处理逻辑
 */
@Slf4j
public class MessageProcessUtil {

    /**
     * 处理MQ消息的通用方法
     *
     * @param message               消息内容
     * @param mqType                MQ类型
     * @param objectMapper          JSON解析器
     * @param channelConfigManager  渠道配置管理器
     * @param messageProcessService 消息处理服务
     */
    public static void processMessage(String message, MqTypeEnum mqType, ObjectMapper objectMapper,
                                      ChannelConfigManager channelConfigManager, IMessageProcessService messageProcessService) {
        try {
            log.info("接收到{}通知消息: {}", mqType.getDesc(), message);

            // 解析消息内容
            Map<String, Object> messageData = objectMapper.readValue(message, Map.class);

            // 根据消息内容处理不同类型的消息
            String notificationId = (String) messageData.get("notificationId");
            String content = (String) messageData.get("content");
            String userId = (String) messageData.get("userId");
            String title = (String) messageData.get("title");
            String channelType = (String) messageData.get("channelType");
            String taskId = (String) messageData.get("taskId");

            // 检查该渠道是否仍使用对应的MQ
            if (!channelConfigManager.isChannelUsingMqType(channelType, mqType.getCode())) {
                log.info("渠道{}已不再使用{}，跳过处理", channelType, mqType.getDesc());
                return;
            }

            // 根据渠道类型处理消息
            if (channelType != null && taskId != null) {
                switch (channelType) {
                    case ChannelTypeEnum.INBOX_CODE:
                        messageProcessService.processInboxMessage(notificationId, title, content, userId, taskId);
                        break;
                    case ChannelTypeEnum.WEBSOCKET_CODE:
                        messageProcessService.processWebsocketMessage(notificationId, title, content, userId, taskId);
                        break;
                    case ChannelTypeEnum.SMS_CODE:
                        messageProcessService.processSmsMessage(notificationId, content, userId, taskId);
                        break;
                    case ChannelTypeEnum.EMAIL_CODE:
                        messageProcessService.processEmailMessage(notificationId, content, userId, taskId);
                        break;
                    case ChannelTypeEnum.APP_PUSH_CODE:
                        messageProcessService.processAppPushMessage(notificationId, content, userId, taskId);
                        break;
                    case ChannelTypeEnum.WECHAT_CODE:
                        messageProcessService.processWechatMessage(notificationId, content, userId, taskId);
                        break;
                    default:
                        log.warn("不支持的渠道类型: {}", channelType);
                        break;
                }
            } else {
                log.warn("消息中缺少必要信息: 渠道类型={} 或 任务ID={}", channelType, taskId);
            }
        } catch (Exception e) {
            log.error("处理{}通知消息失败", mqType.getDesc(), e);
        }
    }
}